package com.eshop.we.common;

import org.apache.commons.lang.StringEscapeUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class ParameterRequestExecuter{
	
	private Map<String, String[]> params = new HashMap<String, String[]>();

	public void modifyParameterValues(String url,Set<String> urls) {// 将parameter的值去除空格后重写回去
		Set<String> set = params.keySet();
		Iterator<String> it = set.iterator();
		while (it.hasNext()) {
			String key = (String) it.next();
			String[] values = params.get(key);
			if (values != null && values.length > 0) {
				if(!urls.contains(url)){
					for (int i = 0; i < values.length; i++) {
						//去除空格 ，防sql和js注入
						values[i] =  StringEscapeUtils.escapeSql(
											//HtmlUtils.htmlEscape(
												//JavaScriptUtils.javaScriptEscape(
														values[0].trim()
												//)
											//)
						);
					}
				}
				params.put(key, values);
			}
		}
	}

	public String getParameter(String name) {// 重写getParameter，代表参数从当前类中的map获取
		String[] values = params.get(name);
		if (values == null || values.length == 0) {
			return null;
		}
		return values[0];
	}

	public String[] getParameterValues(String name) {// 同上
		return params.get(name);
	}

	public void addAllParameters(Map<String, Object> otherParams) {// 增加多个参数
		for (Map.Entry<String, Object> entry : otherParams.entrySet()) {
			addParameter(entry.getKey(), entry.getValue());
		}
	}

	public void addParameter(String name, Object value) {// 增加参数
		if (value != null) {
			if (value instanceof String[]) {
				params.put(name, (String[]) value);
			} else if (value instanceof String) {
				params.put(name, new String[] { (String) value });
			} else {
				params.put(name, new String[] { String.valueOf(value) });
			}
		}
	}

	public Map<String, String[]> getParams() {
		return params;
	}

	public void setParams(Map<String, String[]> params) {
		this.params = params;
	}
}
